package com.other.server;

import com.other.util.CloseUtil;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private ServerSocket server;
    private boolean isShutDown= false;

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    private void start() {
        start(8888);
    }

    private void start(int port) {
        try {
            server = new ServerSocket(port);
            this.receive();
        } catch (IOException e) {
            //e.printStackTrace();
            stop();
        }
    }

    private void receive() throws IOException {
            while (!isShutDown){
                Socket accept = server.accept();
                System.out.println("接受请求");
                new Thread(new Dispatcher(accept)).start();
            }
    }

    private void stop() {
        isShutDown = true;
        CloseUtil.closeSocket(server);
    }
}
